import Vue from 'vue'
import VueRouter from 'vue-router'

// 导入需要的组件
// import Login from '@/components/Login.vue'
const Login = () => import(/* webpackChunkName: "login_home_welcome" */ '@/components/Login.vue')
// import Home from '@/components/Home.vue'
const Home = () => import(/* webpackChunkName: "login_home_welcome" */ '@/components/Home.vue')
// import Welcome from '@/components/Welcome.vue'
const Welcome = () => import(/* webpackChunkName: "login_home_welcome" */ '@/components/Welcome.vue')

// import Users from '@/components/user/Users.vue'
const Users = () => import(/* webpackChunkName: "user_right_roles" */ '@/components/user/Users.vue')
// import Rights from '@/components/power/Rights.vue'
const Rights = () => import(/* webpackChunkName: "user_right_roles" */ '@/components/power/Rights.vue')
// import Roles from '@/components/power/Roles.vue'
const Roles = () => import(/* webpackChunkName: "user_right_roles" */ '@/components/power/Roles.vue')

// import Categories from '@/components/goods/Categories.vue'
const Categories = () => import(/* webpackChunkName: "categories_params" */ '@/components/goods/Categories.vue')
// import Params from '@/components/goods/Params.vue'
const Params = () => import(/* webpackChunkName: "categories_params" */ '@/components/goods/Params.vue')

// import Goods from '@/components/goods/Goods.vue'
const Goods = () => import(/* webpackChunkName: "goods" */ '@/components/goods/Goods.vue')
// import Add from '@/components/goods/Add.vue'
const Add = () => import(/* webpackChunkName: "goods" */ '@/components/goods/Add.vue')
// import Edit from '@/components/goods/Edit.vue'
const Edit = () => import(/* webpackChunkName: "goods" */ '@/components/goods/Edit.vue')

// import Orders from '@/components/order/Orders.vue'
const Orders = () => import(/* webpackChunkName: "order_report" */ '@/components/order/Orders.vue')
// import Report from '@/components/report/Report.vue'
const Report = () => import(/* webpackChunkName: "order_report" */ '@/components/report/Report.vue')

// 将路由注册为 vue 插件
Vue.use(VueRouter)

const routes = [
  // 重定向
  { path: '/', redirect: '/login' },
  { path: '/login', component: Login },
  {
    path: '/home',
    component: Home,
    redirect: '/welcome',
    children: [
      { path: '/welcome', component: Welcome },
      { path: '/users', component: Users },
      { path: '/rights', component: Rights },
      { path: '/roles', component: Roles },
      { path: '/categories', component: Categories },
      { path: '/params', component: Params },
      { path: '/goods', component: Goods },
      { path: '/goods/add', component: Add },
      { path: '/orders', component: Orders },
      { path: '/reports', component: Report },
      { path: '/goods/:id', component: Edit, props: true }
    ]
  }
]

// 创建路由实例，并创建路由规则
const router = new VueRouter({
  routes
})

// 全局前置守卫
router.beforeEach((to, from, next) => {
  // 如果访问的是 登录页面 直接放行
  if (to.path === '/login') return next()
  // 如果不是登录页面，获取保存到浏览器中的 token 值
  const tokenStr = sessionStorage.getItem('token')
  // 判断token是否正确，如果不正确，强制跳转到登录页面
  if (!tokenStr) return next('/login')
  // token值正确放行当前跳转
  next()
})

// 对外导出路由模块
export default router
